const socket = new WebSocket('ws://127.0.0.1:9002');

socket.onopen = function(){
    console.log("连接成功~");
}

socket.onmessage = function(event)
{
    const message = event.data;
    console.log('message: ${message}');
}


var targetNode=document.getElementsByClassName("webcast-chatroom___items")[0].children[0];

var options = { attributes: false, childList: true,subtree:true,attributeOldValue:false};

function callback(mutationsList, observer) 
{ 
    if(mutationsList[mutationsList.length-1].addedNodes.length>0){ 
        console.log(mutationsList[mutationsList.length-1].addedNodes[0].innerText)    
    if (socket.readyState === WebSocket.OPEN)
        socket.send(mutationsList[mutationsList.length-1].addedNodes[0].innerText)
    };
}

//生成监听方法
var mutationObserver = new MutationObserver(callback);
//执行监听
mutationObserver.observe(targetNode, options);